﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace Fa.Kanbox.API
{
    public class KbAddress
    {
        public const string Auth = "https://auth.kanbox.com/0/auth";
        public const string Token = "https://auth.kanbox.com/0/token";


        public const string Info = "https://api.kanbox.com/0/info";
        public const string List = "https://api.kanbox.com/0/list";
        public const string Download = "https://api.kanbox.com/0/download";
        public const string Upload = "https://api-upload.kanbox.com/0/upload";
        public const string Delete = "https://api.kanbox.com/0/delete";
        public const string Move = "https://api.kanbox.com/0/move";
        public const string CreateFolder = "https://api.kanbox.com/0/create_folder";
        /// <summary>创建共享</summary>
        public const string Share = "https://api.kanbox.com/0/share";
        /// <summary>共享邀请</summary>
        public const string PendingShares = "https://api.kanbox.com/0/pendingshares";


        private static string _RedirectUri = "https://www.kanbox.com";
        public static string RedirectUri
        {
            get
            {
                return _RedirectUri;
            }
            set
            {
                RedirectUri = value;
            }
        }

        public static Dictionary<string, object> ObjectToDictionary(object obj)
        {
            return obj.GetType().GetProperties().ToDictionary(p => p.Name, p => p.GetValue(obj, new object[0]));
        }

        public static string GetUrlData(object obj)
        {
            return GetUrlData(ObjectToDictionary(obj));
        }
        public static string GetUrlData(Dictionary<string, object> dict)
        {
            var arr = new string[dict.Keys.Count];
            var i = 0;
            foreach (var item in dict)
            {
                arr[i] = string.Format("{0}={1}", item.Key, item.Value == null ? string.Empty : System.Web.HttpUtility.UrlEncode(item.Value.ToString()));
                i++;
            }
            return string.Join("&", arr);
        }
        public static string Combine(string url, Dictionary<string, object> ps)
        {
            return url + "?" + GetUrlData(ps);
        }
        public static string Combine(string url, object obj)
        {
            return Combine(url, ObjectToDictionary(obj));
        }

    }
}
